package com.ruoyi.yuan.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.yuan.domain.Nation;
import com.ruoyi.yuan.mapper.MainlandMapper;
import com.ruoyi.yuan.domain.Mainland;
import com.ruoyi.yuan.service.IMainlandService;

/**
 * 大陆信息Service业务层处理
 * 
 * @author guifeng
 * @date 2022-08-25
 */
@Service
public class MainlandServiceImpl implements IMainlandService 
{
    @Autowired
    private MainlandMapper mainlandMapper;

    /**
     * 查询大陆信息
     * 
     * @param mainlandId 大陆信息主键
     * @return 大陆信息
     */
    @Override
    public Mainland selectMainlandByMainlandId(Long mainlandId)
    {
        return mainlandMapper.selectMainlandByMainlandId(mainlandId);
    }

    /**
     * 查询大陆信息列表
     * 
     * @param mainland 大陆信息
     * @return 大陆信息
     */
    @Override
    public List<Mainland> selectMainlandList(Mainland mainland)
    {
        return mainlandMapper.selectMainlandList(mainland);
    }

    /**
     * 新增大陆信息
     * 
     * @param mainland 大陆信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertMainland(Mainland mainland)
    {
        int rows = mainlandMapper.insertMainland(mainland);
        insertNation(mainland);
        return rows;
    }

    /**
     * 修改大陆信息
     * 
     * @param mainland 大陆信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateMainland(Mainland mainland)
    {
        mainlandMapper.deleteNationByMainlandId(mainland.getMainlandId());
        insertNation(mainland);
        return mainlandMapper.updateMainland(mainland);
    }

    /**
     * 批量删除大陆信息
     * 
     * @param mainlandIds 需要删除的大陆信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMainlandByMainlandIds(Long[] mainlandIds)
    {
        mainlandMapper.deleteNationByMainlandIds(mainlandIds);
        return mainlandMapper.deleteMainlandByMainlandIds(mainlandIds);
    }

    /**
     * 删除大陆信息信息
     * 
     * @param mainlandId 大陆信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMainlandByMainlandId(Long mainlandId)
    {
        mainlandMapper.deleteNationByMainlandId(mainlandId);
        return mainlandMapper.deleteMainlandByMainlandId(mainlandId);
    }

    /**
     * 新增国家信息信息
     * 
     * @param mainland 大陆信息对象
     */
    public void insertNation(Mainland mainland)
    {
        List<Nation> nationList = mainland.getNationList();
        Long mainlandId = mainland.getMainlandId();
        if (StringUtils.isNotNull(nationList))
        {
            List<Nation> list = new ArrayList<Nation>();
            for (Nation nation : nationList)
            {
                nation.setMainlandId(mainlandId);
                list.add(nation);
            }
            if (list.size() > 0)
            {
                mainlandMapper.batchNation(list);
            }
        }
    }
}
